package giro.general.team;

import giro.general.ListBaseActivity;
import giro.general.R;
import giro.general.adapter.DettagliSquadreArrayAdapter;
import giro.general.util.Commons;
import giro.general.util.Parser;

import org.json.JSONArray;
import org.json.JSONException;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class DettagliSquadra extends ListBaseActivity {
	private JSONArray squadreArray = null;
	private String[] corridori = null;

	// private String[] corridori = new String[] { "Corr1", "Corr2", "Corr3" };

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dettagli_squadre);
		initToolbar();

		Intent intent = getIntent(); // l'intent di questa activity

		String pkg = getPackageName();

		String nomeSquadra = intent.getStringExtra(pkg + ".squadra"); // prendiamo
																		// i
																		// dati
		TextView textview = (TextView) findViewById(R.id.nome_squadra);
		textview.setText(nomeSquadra + ":");

		// creaArrayJson();
		// squadreArray=Parser.getInstance().createArrayJson(this,
		// R.raw.squadre);
		// Commons.getInstance().setSquadreArray(squadreArray);
		// Parser par = new Parser();
		try {
			// corridori = par.parseCorridori(Commons.getInstance()
			// .getsquadreArray(), nomeSquadra);
			Parser.getInstance().parseCorridori(Commons.getInstance().getsquadreArray(), nomeSquadra);
			String[] temp = Commons.getInstance().getCorridore();
			int count = 0;
			for (int i = 0; i < temp.length; i++) {
				if (temp[i] != null) {
					count++;
				}
			}

			corridori = new String[count];
			for (int i = 0; i < temp.length; i++) {
				if (temp[i] != null) {
					corridori[i] = temp[i];
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		DettagliSquadreArrayAdapter adapter = new DettagliSquadreArrayAdapter(this, corridori);
		setListAdapter(adapter);

		final ListView lv = getListView();
		lv.setTextFilterEnabled(true);

		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				Intent intent = new Intent(getApplicationContext(), DettagliCorridore.class);
				String pkg = getPackageName();
				Object ob = new Object();
				ob = lv.getItemAtPosition(position);

				intent.putExtra(pkg + ".position", String.valueOf(position));
				intent.putExtra(pkg + ".corridore", ob.toString());
				startActivity(intent);

			}
		});

	}
	/*
	 * public void creaArrayJson() {
	 * 
	 * InputStream is = getResources().openRawResource(R.raw.squadre); Writer
	 * writer = new StringWriter(); char[] buffer = new char[1024]; try { Reader
	 * reader = null; reader = new BufferedReader(new InputStreamReader(is,
	 * "UTF-8")); int n; while ((n = reader.read(buffer)) != -1) {
	 * writer.write(buffer, 0, n); } } catch (IOException e) {
	 * e.printStackTrace(); } finally { try { is.close(); } catch (IOException
	 * e) { e.printStackTrace(); } }
	 * 
	 * try { squadreArray = new JSONArray(writer.toString()); } catch
	 * (JSONException e) { e.printStackTrace(); }
	 * 
	 * Commons.getInstance().setSquadreArray(squadreArray);
	 * 
	 * }
	 */
}